package com.neutech.base;

import com.neutech.Util.ImageUtils;
import com.neutech.constant.Constant;
import com.neutech.frame.TankFrame;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

public class TankSeletor  implements Drawable {

    private Image image;
    private int x;
    private  int y;
    private TankMap tankMap;
    /**
     * 要用tankframe里面的启动项，用引用，先申明一个tankframe
     */
    private TankFrame tankFrame;/* 第一步  申明*/

    private List<Image> tankImage = new ArrayList<>();
    /*第二步  下面传进去上面申明的tankframe*/
    public TankSeletor(TankFrame tankFrame) {
    loadingImage();
    this.tankFrame = tankFrame;/*第三步  在家一个中国*//*第四步在Tankframe*/
    init();
    }
    public void music(String path){
        new Thread(new Runnable() {
            @Override
            public void run() {
                FileInputStream is=null;
                AudioStream os=null;
                try {
                    is = new FileInputStream(path);
                    os = new AudioStream(is);
                    AudioPlayer.player.start(os);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    /**
     * 初始化的方法
     */
    public void init(){
   x = Constant.SELECT_TANK_LEFT;
   y = Constant.SELECT_TANK_TOP;
    }
 private int index;
    @Override
    public void draw(Graphics g) {
     g.drawImage(image,0,0,
             Constant.FRAME_WIDTH,
             Constant.FRAME_HEIGHT,null);
     g.drawImage(tankImage.get(index++),x,y,null);
     if (index >1){
         index = 0;
     }



        g.setColor(Color.white);
        g.setFont(new Font("楷体",Font.BOLD,28));
        g.drawString("选择" + "：" ,30,200);
        g.drawString("按键E" ,400,200);
        g.drawString("确定" + "：" ,30,250);
        g.drawString("按键Q" ,400,250);



    }

    public void loadingImage(){
    image = ImageUtils.getValue("tank_main");
    tankImage .add(ImageUtils.getValue("tank_y_1_3"));
    tankImage .add(ImageUtils.getValue("tank_y_1_4"));
    }
    public void keyPressed(KeyEvent e){
        /**
         * 暂停  Q
         */
    if (e.getKeyCode() == KeyEvent.VK_Q){
        //268是选择p1.300是选择p2,332是选择自定义地图

        if (y == 268){
            music("src/com/neutech/music/intro.wav");
            //状态码 tankframe里面的
           tankFrame.changeStatus(2);
        }
        else if (y == 300){
            tankFrame.changeStatus(3);
        }else if (y == 332){
            tankFrame.changeStatus(4);

        }
    }
    /**
     * 选择键   E
     */
    else if (e.getKeyCode() == KeyEvent.VK_E){
        y+=Constant.SPRITE_SIZE;
        if (y> 332){
            y = Constant.SELECT_TANK_TOP;
        }
    }

    }

    public void keyReleased(KeyEvent e){

    }


}
